package com.example.administrator.bdsjws.activities;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.MotionEventCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.example.administrator.bdsjws.MyApplication;
import com.example.administrator.bdsjws.R;
import com.example.administrator.bdsjws.activities.helper.ItemTouchHelperAdapter;
import com.example.administrator.bdsjws.activities.helper.OnStartDragListener;
import com.example.administrator.bdsjws.vo.BBX;

import java.util.Collections;
import java.util.List;

/**
 * Created by Administrator on 2017/5/24 0024.
 */

public class MyRecalAdapter extends RecyclerView.Adapter<MyRecalAdapter.ViewHolder> implements ItemTouchHelperAdapter {

    private List<BBX> list;
    private final OnStartDragListener mDragStartListener;
    private View itemView;

    public MyRecalAdapter(List<BBX> list, OnStartDragListener dragStartListener){
        this.list=list;
        mDragStartListener = dragStartListener;
    }



    public static class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView imageView;
        private TextView tv_item;

        public ViewHolder(View itemView) {
            super(itemView);
            imageView= (ImageView) itemView.findViewById(R.id.imeges);
            tv_item = (TextView) itemView.findViewById(R.id.texts);
        }
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.recal_item,parent,false);

        MyRecalAdapter.ViewHolder viewHolder = new MyRecalAdapter.ViewHolder(itemView);

        return viewHolder;

    }


    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        BBX bbx= (BBX) list.get(position);
        final Context context= MyApplication.getContext();
        int resid=context.getResources().getIdentifier(bbx.getIcon(),"mipmap",context.getPackageName());
        Drawable drawable= ContextCompat.getDrawable(context,resid);
        holder.tv_item.setText(bbx.getUsername());
        holder.imageView.setImageDrawable(drawable);
        holder.imageView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
                    mDragStartListener.onStartDrag(holder);
                }
                return false;
            }
        });
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (position){
                    case 6:
                        Intent intent=new Intent(context,Jiarenshouhu.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(intent);
                        break;
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onItemDismiss(int position) {
        list.remove(position);
        notifyItemRemoved(position);
    }

    @Override
    public boolean onItemMove(int fromPosition, int toPosition) {
        Collections.swap(list, fromPosition, toPosition);
        notifyItemMoved(fromPosition, toPosition);
        return true;
    }
}
